package com.ky.manage.activityLifecycle;

import android.app.Activity;
import android.os.Build;

import java.lang.ref.WeakReference;
import java.util.Stack;

/**
 * Activity栈管理类
 *
 * @author zhaoyan
 * @date 2019-11-06
 */
public class ActivityManagerUtil {
    private static volatile ActivityManagerUtil activityManagerUtil;

    private Stack<WeakReference<Activity>> mActivityStack;

    private ActivityManagerUtil() {
        mActivityStack = new Stack<>();
    }

    public static ActivityManagerUtil getInstance() {
        if (activityManagerUtil == null) {
            synchronized (ActivityManagerUtil.class) {
                if (activityManagerUtil == null) {
                    activityManagerUtil = new ActivityManagerUtil();
                }
            }
        }
        return activityManagerUtil;
    }

    /**
     * 获取指定的Activity
     */
    public Activity getActivity(Class<?> cls) {
        if (cls == null || isActivityStackEmpty()) {
            return null;
        }
        for (WeakReference<Activity> weakActivity : mActivityStack) {
            if (null != weakActivity) {
                if (cls.equals(weakActivity.get() == null ? null : weakActivity.get().getClass())) {
                    return weakActivity.get();
                }
            }
        }
        return null;
    }

    /**
     * 判断activity栈是否为空
     *
     * @return
     */
    public boolean isActivityStackEmpty() {
        return null == mActivityStack || mActivityStack.isEmpty();
    }

    /**
     * 添加Activity到堆栈
     */
    public void push(Activity activity) {
        if (activity == null) {
            return;
        }
        if (null == mActivityStack) {
            mActivityStack = new Stack<>();
        }
        WeakReference<Activity> weakActivity = new WeakReference<>(activity);
        mActivityStack.push(weakActivity);
    }

    /**
     * 从堆栈移除activity
     */
    public void remove(Activity activity) {
        if (activity == null || isActivityStackEmpty()) {
            return;
        }
        for (WeakReference<Activity> weakActivity : mActivityStack) {
            if (null != weakActivity) {
                if (weakActivity.get() == activity) {
                    mActivityStack.remove(weakActivity);
                    break;
                }
            }
        }
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public Activity lastActivity() {
        if (isActivityStackEmpty()) {
            return null;
        }
        return mActivityStack.lastElement().get();
    }

    /**
     * 获取first Activity（堆栈中第一个压入的）
     */
    public Activity firstActivity() {
        if (isActivityStackEmpty()) {
            return null;
        }
        return mActivityStack.firstElement().get();
    }

    /**
     * 判断某个activity是否存在
     *
     * @return
     */
    public boolean isHaveActivity(Class<?> cls) {
        if (null == cls || isActivityStackEmpty()) {
            return false;
        }
        for (WeakReference<Activity> weakActivity : mActivityStack) {
            if (null != weakActivity) {
                if (cls.equals(weakActivity.get() == null ? null : weakActivity.get().getClass())) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 结束指定的Activity
     */
    public void finishAndRemoveActivity(Activity activity) {
        if (activity == null || isActivityStackEmpty()) {
            return;
        }
        for (WeakReference<Activity> weakActivity : mActivityStack) {
            if (null != weakActivity) {
                if (weakActivity.get() == activity) {
                    mActivityStack.remove(weakActivity);
                    activity.finish();
                    break;
                }
            }
        }
    }

    /**
     * 结束指定类名的Activity（所有同类名的都会被取消）
     */
    public void finishActivity(Class<?> cls) {
        if (cls == null || isActivityStackEmpty()) {
            return;
        }
        for (WeakReference<Activity> weakActivity : mActivityStack) {
            if (null != weakActivity) {
                if (cls.equals(weakActivity.get() == null ? null : weakActivity.get().getClass())) {
                    mActivityStack.remove(weakActivity);
                    Activity activity = weakActivity.get();
                    if (activity != null) {
                        activity.finish();
                    }
                }
            }
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        if (isActivityStackEmpty()) {
            return;
        }
        WeakReference<Activity> topWeakActivity;
        while ((topWeakActivity = mActivityStack.pop()) != null) {
            if (topWeakActivity.get() != null) {
                topWeakActivity.get().finish();
            }
        }
    }

    /**
     * activity是否销毁了
     *
     * @return
     */
    public boolean isActivityDestroy(Activity ownerActivity) {
        if (null == ownerActivity) {
            return true;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return ownerActivity.isFinishing() || ownerActivity.isDestroyed();
        } else {
            return ownerActivity.isFinishing();
        }
    }
}
